import 'package:flutter/material.dart';
import 'package:fwanandroid/ui/main/ContentPageView.dart';
import 'package:fwanandroid/ui/views/BottomBarWidget.dart';
import 'package:toast/toast.dart';

class MainPage extends StatefulWidget {
  String title = "首页";

  MainPage({super.key}) {}

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _currentIndex = 0;
  late PageController _pageController;

  @override
  void initState() {
    ToastContext().init(context);
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void _onPageChanged(int index) {
    // 页面改变时更新下标状态
    setState(() {
      _currentIndex = index;
    });
  }

  void _onItemTapped(int selectedIndex) {
    _pageController.jumpToPage(selectedIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            ContentPageView(
                pageController: _pageController, onPageChanged: _onPageChanged),
            BottomBarWidget(
                currentIndex: _currentIndex, onItemSelected: _onItemTapped)
          ],
        ),
      ),
    );
  }
}
